/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-08     31603       the first version
 */



#include <OLED_DEV.h>
#include "FACE_DATA.h"


#define OLED_I2C_BUS_NAME "i2c1"
#define OLED_ADDR 0x3c //从机地址 0011 1100 原来是0x78 右移1位

static struct rt_i2c_bus_device *i2c_bus=RT_NULL;




rt_err_t WriteCmd(struct rt_i2c_bus_device *bus, rt_uint8_t data)
{
    rt_uint8_t buf[2];
    struct rt_i2c_msg msgs;

    buf[0] = 0x00;
    buf[1] = data;


    msgs.addr = OLED_ADDR;
    msgs.flags = RT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    /* 调用I2C设备接口传输数据 */
    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
    {
        return RT_EOK;
    }
    else
    {
        return -RT_ERROR;
    }
}

rt_err_t WriteDat(struct rt_i2c_bus_device *bus, rt_uint8_t data)
{
    rt_uint8_t buf[2];
    struct rt_i2c_msg msgs;

    buf[0] = 0x40;
    buf[1] = data;


    msgs.addr = OLED_ADDR;
    msgs.flags = RT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    /* 调用I2C设备接口传输数据 */
    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
    {
        return RT_EOK;
    }
    else
    {
        return -RT_ERROR;
    }
}

void OLED_INIT(void)
{
    rt_thread_mdelay(100);

    WriteCmd(i2c_bus,0xAE); //显示关闭
    WriteCmd(i2c_bus,0x20);
    WriteCmd(i2c_bus,0x10);
    WriteCmd(i2c_bus,0xb0);

    WriteCmd(i2c_bus,0xc8); //Set COM Output Scan Direction

    WriteCmd(i2c_bus,0x00);
    WriteCmd(i2c_bus,0x10);
    WriteCmd(i2c_bus,0x40);
    WriteCmd(i2c_bus,0x81);
    WriteCmd(i2c_bus,0xff); //亮度调节 0x00~0xff
    WriteCmd(i2c_bus,0xa1);
    WriteCmd(i2c_bus,0xa6);
    WriteCmd(i2c_bus,0xa8);
    WriteCmd(i2c_bus,0x3F);
    WriteCmd(i2c_bus,0xa4);
    WriteCmd(i2c_bus,0xd3);
    WriteCmd(i2c_bus,0x00);
    WriteCmd(i2c_bus,0xd5);
    WriteCmd(i2c_bus,0xf0);
    WriteCmd(i2c_bus,0xd9);
    WriteCmd(i2c_bus,0x22);
    WriteCmd(i2c_bus,0xda);
    WriteCmd(i2c_bus,0x12);
    WriteCmd(i2c_bus,0xdb);
    WriteCmd(i2c_bus,0x20);
    WriteCmd(i2c_bus,0x8d);
    WriteCmd(i2c_bus,0x14); //
    WriteCmd(i2c_bus,0xaf); //--turn on oled panel

}

//全屏填充
void OLED_FILL(unsigned char fill_data)
{
    unsigned char m,n;
    for(m=0;m<8;m++){
        WriteCmd(i2c_bus,0xb0+m);
        WriteCmd(i2c_bus,0x00);
        WriteCmd(i2c_bus,0x10);

        for(n=0;n<128;n++){
            WriteDat(i2c_bus,fill_data);
        }
    }
}

//OLED清屏
void OLED_CLS(void)
{
    OLED_FILL(0x00);
}

//设置起始点坐标
void OLED_SetPos(unsigned char x, unsigned char y)
{
    WriteCmd(i2c_bus,0xb0+y);
    WriteCmd(i2c_bus,((x&0xf0)>>4)|0x10);
    WriteCmd(i2c_bus,(x&0x0f)|0x01);
}

const unsigned char F6x8[][6] =
{
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},// sp
    {0x00, 0x00, 0x00, 0x2f, 0x00, 0x00},// !
    {0x00, 0x00, 0x07, 0x00, 0x07, 0x00},// "
    {0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14},// #
    {0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12},// $
    {0x00, 0x62, 0x64, 0x08, 0x13, 0x23},// %
    {0x00, 0x36, 0x49, 0x55, 0x22, 0x50},// &
    {0x00, 0x00, 0x05, 0x03, 0x00, 0x00},// '
    {0x00, 0x00, 0x1c, 0x22, 0x41, 0x00},// (
    {0x00, 0x00, 0x41, 0x22, 0x1c, 0x00},// )
    {0x00, 0x14, 0x08, 0x3E, 0x08, 0x14},// *
    {0x00, 0x08, 0x08, 0x3E, 0x08, 0x08},// +
    {0x00, 0x00, 0x00, 0xA0, 0x60, 0x00},// ,
    {0x00, 0x08, 0x08, 0x08, 0x08, 0x08},// -
    {0x00, 0x00, 0x60, 0x60, 0x00, 0x00},// .
    {0x00, 0x20, 0x10, 0x08, 0x04, 0x02},// /
    {0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E},// 0
    {0x00, 0x00, 0x42, 0x7F, 0x40, 0x00},// 1
    {0x00, 0x42, 0x61, 0x51, 0x49, 0x46},// 2
    {0x00, 0x21, 0x41, 0x45, 0x4B, 0x31},// 3
    {0x00, 0x18, 0x14, 0x12, 0x7F, 0x10},// 4
    {0x00, 0x27, 0x45, 0x45, 0x45, 0x39},// 5
    {0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30},// 6
    {0x00, 0x01, 0x71, 0x09, 0x05, 0x03},// 7
    {0x00, 0x36, 0x49, 0x49, 0x49, 0x36},// 8
    {0x00, 0x06, 0x49, 0x49, 0x29, 0x1E},// 9
    {0x00, 0x00, 0x36, 0x36, 0x00, 0x00},// :
    {0x00, 0x00, 0x56, 0x36, 0x00, 0x00},// ;
    {0x00, 0x08, 0x14, 0x22, 0x41, 0x00},// <
    {0x00, 0x14, 0x14, 0x14, 0x14, 0x14},// =
    {0x00, 0x00, 0x41, 0x22, 0x14, 0x08},// >
    {0x00, 0x02, 0x01, 0x51, 0x09, 0x06},// ?
    {0x00, 0x32, 0x49, 0x59, 0x51, 0x3E},// @
    {0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C},// A
    {0x00, 0x7F, 0x49, 0x49, 0x49, 0x36},// B
    {0x00, 0x3E, 0x41, 0x41, 0x41, 0x22},// C
    {0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C},// D
    {0x00, 0x7F, 0x49, 0x49, 0x49, 0x41},// E
    {0x00, 0x7F, 0x09, 0x09, 0x09, 0x01},// F
    {0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A},// G
    {0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F},// H
    {0x00, 0x00, 0x41, 0x7F, 0x41, 0x00},// I
    {0x00, 0x20, 0x40, 0x41, 0x3F, 0x01},// J
    {0x00, 0x7F, 0x08, 0x14, 0x22, 0x41},// K
    {0x00, 0x7F, 0x40, 0x40, 0x40, 0x40},// L
    {0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F},// M
    {0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F},// N
    {0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E},// O
    {0x00, 0x7F, 0x09, 0x09, 0x09, 0x06},// P
    {0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E},// Q
    {0x00, 0x7F, 0x09, 0x19, 0x29, 0x46},// R
    {0x00, 0x46, 0x49, 0x49, 0x49, 0x31},// S
    {0x00, 0x01, 0x01, 0x7F, 0x01, 0x01},// T
    {0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F},// U
    {0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F},// V
    {0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F},// W
    {0x00, 0x63, 0x14, 0x08, 0x14, 0x63},// X
    {0x00, 0x07, 0x08, 0x70, 0x08, 0x07},// Y
    {0x00, 0x61, 0x51, 0x49, 0x45, 0x43},// Z
    {0x00, 0x00, 0x7F, 0x41, 0x41, 0x00},// [
    {0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55},// 55
    {0x00, 0x00, 0x41, 0x41, 0x7F, 0x00},// ]
    {0x00, 0x04, 0x02, 0x01, 0x02, 0x04},// ^
    {0x00, 0x40, 0x40, 0x40, 0x40, 0x40},// _
    {0x00, 0x00, 0x01, 0x02, 0x04, 0x00},// '
    {0x00, 0x20, 0x54, 0x54, 0x54, 0x78},// a
    {0x00, 0x7F, 0x48, 0x44, 0x44, 0x38},// b
    {0x00, 0x38, 0x44, 0x44, 0x44, 0x20},// c
    {0x00, 0x38, 0x44, 0x44, 0x48, 0x7F},// d
    {0x00, 0x38, 0x54, 0x54, 0x54, 0x18},// e
    {0x00, 0x08, 0x7E, 0x09, 0x01, 0x02},// f
    {0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C},// g
    {0x00, 0x7F, 0x08, 0x04, 0x04, 0x78},// h
    {0x00, 0x00, 0x44, 0x7D, 0x40, 0x00},// i
    {0x00, 0x40, 0x80, 0x84, 0x7D, 0x00},// j
    {0x00, 0x7F, 0x10, 0x28, 0x44, 0x00},// k
    {0x00, 0x00, 0x41, 0x7F, 0x40, 0x00},// l
    {0x00, 0x7C, 0x04, 0x18, 0x04, 0x78},// m
    {0x00, 0x7C, 0x08, 0x04, 0x04, 0x78},// n
    {0x00, 0x38, 0x44, 0x44, 0x44, 0x38},// o
    {0x00, 0xFC, 0x24, 0x24, 0x24, 0x18},// p
    {0x00, 0x18, 0x24, 0x24, 0x18, 0xFC},// q
    {0x00, 0x7C, 0x08, 0x04, 0x04, 0x08},// r
    {0x00, 0x48, 0x54, 0x54, 0x54, 0x20},// s
    {0x00, 0x04, 0x3F, 0x44, 0x40, 0x20},// t
    {0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C},// u
    {0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C},// v
    {0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C},// w
    {0x00, 0x44, 0x28, 0x10, 0x28, 0x44},// x
    {0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C},// y
    {0x00, 0x44, 0x64, 0x54, 0x4C, 0x44},// z
    {0x14, 0x14, 0x14, 0x14, 0x14, 0x14}// horiz lines
};
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[])
{
    unsigned char c = 0,i = 0,j = 0;
    while(ch[j] != '\0')
    {
        c = ch[j] - 32;
        if(x > 126)
        {
            x = 0;
            y++;
        }
        OLED_SetPos(x,y);
        for(i=0;i<6;i++)
            WriteDat(i2c_bus,F6x8[c][i]);
        x += 6;
        j++;
    }
}
void OLED_ShowImage(const uint8_t *image)
{
    uint8_t page, col;
    for(page = 0; page < 8; page++) {        // 遍历所有页（0-7）
        WriteCmd(i2c_bus, 0xB0 + page);     // 设置当前页地址
        WriteCmd(i2c_bus, 0x00);            // 设置列地址低4位
        WriteCmd(i2c_bus, 0x10);            // 设置列地址高4位

        for(col = 0; col < 128; col++) {    // 遍历当前页的128列
            WriteDat(i2c_bus, image[page * 128 + col]); // 写入图像数据
        }
    }
}
void OLED_ALL_init(void)
{
    rt_err_t result=RT_NULL;
    i2c_bus=(struct rt_i2c_bus_device *)rt_device_find(OLED_I2C_BUS_NAME);
    if(i2c_bus==RT_NULL){
        rt_kprintf("no device \n");
    }else{
        rt_kprintf("find device \n");

        //初始化程序段
        OLED_INIT();
        OLED_CLS();

        OLED_ShowImage(Face_sleep);
    }

}






















